package com.atguigu.service.impl;

import com.atguigu.entity.PlatformPropertyKey;
import com.atguigu.entity.PlatformPropertyValue;
import com.atguigu.mapper.PlatformPropertyKeyMapper;
import com.atguigu.service.PlatformPropertyKeyService;
import com.atguigu.service.PlatformPropertyValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 属性表 服务实现类
 * </p>
 *
 * @author yangchaolove
 * @since 2021-11-07
 */
@Service
public class PlatformPropertyKeyServiceImpl extends ServiceImpl<PlatformPropertyKeyMapper, PlatformPropertyKey> implements PlatformPropertyKeyService {

    @Autowired
    private PlatformPropertyValueService propertyValueService;
    @Override
    public List<PlatformPropertyKey> getPlatformPropertyByCategoryId(Long category1Id, Long category2Id, Long category3Id) {

        //方式一 采用传统方式来写这个代码 一个一个的查 然后进行封装
        List<PlatformPropertyKey> propertyKeyList = baseMapper.getPropertyKeyByCategoryId(category1Id, category2Id, category3Id);
        for (PlatformPropertyKey platformPropertyKey : propertyKeyList) {
            List<PlatformPropertyValue> propertyValueList = propertyValueService.getPropertyValueByPropertyKeyId(platformPropertyKey.getId());
            platformPropertyKey.setPropertyValueList(propertyValueList);
        }
        return propertyKeyList;
        //方式二 优化 sql
//        return baseMapper.getPlatformPropertyByCategoryId(category1Id,category2Id,category3Id);

    }

    @Override
    public void savePlatformProperty(PlatformPropertyKey platformPropertyKey) {
        //根据有无id 判断是新增还是修改
        if(platformPropertyKey.getId()!=null){
            baseMapper.updateById(platformPropertyKey);
        }else{
            baseMapper.insert(platformPropertyKey);
        }
        //删除原来的平台属性value
        QueryWrapper<PlatformPropertyValue> wrapper = new QueryWrapper<>();
        wrapper.eq("property_key_id",platformPropertyKey.getId());
        propertyValueService.remove(wrapper);


        List<PlatformPropertyValue> propertyValueList = platformPropertyKey.getPropertyValueList();
        if(propertyValueList!=null){
            for(PlatformPropertyValue platformPropertyValue:propertyValueList){
                platformPropertyValue.setPropertyKeyId(platformPropertyKey.getId());
                //sql优化 减少数据库存入的次数
//                propertyValueService.save(platformPropertyValue);
            }
        }
        propertyValueService.saveBatch(propertyValueList);
    }

    @Override
    public void deletePlatformProperty(Long id) {
        //先删除平台value数据
        List<PlatformPropertyValue> list = propertyValueService.getPropertyValueByPropertyKeyId(id);
        List<Long> collect = list.stream().map(e -> {
            return e.getId();
        }).collect(Collectors.toList());
        propertyValueService.removeByIds(collect);
        baseMapper.deleteById(id);

    }

    @Override
    public List<PlatformPropertyKey> getPlatformPropertyBySkuId(Long skuId) {
        return baseMapper.getPlatformPropertyBySkuId(skuId);
    }
}
